<template>
  <div class="container">
    <ASteps :current="step">
      <AStep title="设置登录信息" />
      <AStep title="设置基本信息" />
      <AStep title="完成" />
    </ASteps>

    <div class="content">
      <!-- 设置登录信息 -->
      <template v-if="step == 0">
        <el-form
          :ref="(e) => registerStart.formRef = e"
          :rules="registerStart.rules"
          :model="registerStart.data"
        >
          <el-form-item prop="mobilePhone" style="margin-bottom:24px">
            <div class="field">
              <el-input
                size="large"
                v-model="registerStart.data.mobilePhone"
                placeholder="请输入11位手机号"
              ></el-input>
            </div>
          </el-form-item>

          <el-form-item prop="verCode" style="margin-bottom:14px">
            <div class="field">
              <el-input v-model="registerStart.data.verCode" size="large" placeholder="请输入验证码"></el-input>
              <el-button
                @click="registerStart.sendVerCode"
                :disabled="registerStart.disabledSendVerCode"
                style="width:165px;margin-left:8px"
                type="primary"
              >发送验证码</el-button>
            </div>
          </el-form-item>

          <el-form-item prop="verCode" style="margin-bottom:20px">
            <el-checkbox v-model="registerStart.data.read">
              注册即表示您已阅读、理解并同意
              <el-button link>《及时通隐私政策》</el-button>
            </el-checkbox>
          </el-form-item>

          <el-button
            class="main-btn"
            @click="registerStart.submit"
            :disabled="registerStart.submitButtonLock"
            type="primary"
          >注 册</el-button>
          <div class="field toRegister">
            <div class="toRegister">
              <div>已有账号?</div>
              <div style="margin-left:4px">
                <el-button @click="toLogin" size="large" link>
                  直接登录
                  <i style="margin-left:10px" class="el-icon-circle-plus-outline"></i>
                </el-button>
              </div>
            </div>
          </div>
        </el-form>
      </template>

      <!-- 设置基本信息 -->
      <template v-if="step == 1">
        <el-form
          :ref="(e) => registerFinish.formRef = e"
          :rules="registerFinish.rules"
          :model="registerFinish.data"
        >
          <div class="field-title">设置账号信息</div>
          <el-form-item prop="mobilePhone" style="margin-bottom:24px">
            <div class="field">
              <el-input size="large" v-model="registerFinish.data.RealName" placeholder="请输入您的真实姓名"></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="Email" style="margin-bottom:32px">
            <div class="field">
              <el-input size="large" v-model="registerFinish.data.Email" placeholder="请输入您的邮箱"></el-input>
            </div>
          </el-form-item>
          <div class="field-title">设置密码</div>

          <el-form-item prop="Password" style="margin-bottom:24px">
            <div class="field">
              <el-input
                size="large"
                show-password
                v-model="registerFinish.data.Password"
                placeholder="请输入您的密码"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="RepeatPassword" style="margin-bottom:32px">
            <div class="field">
              <el-input
                size="large"
                show-password
                v-model="registerFinish.data.RepeatPassword"
                placeholder="确认密码"
              ></el-input>
            </div>
          </el-form-item>

          <el-button class="submit-finish-btn" @click="registerFinish.submit" type="primary">提交资料</el-button>
        </el-form>
      </template>
    </div>
    <!-- 成功 -->
    <template v-if="step == 2">
      <el-result class="register-success" icon="success" :title="successTitle">
        <template #extra>
          <el-button
            type="primary"
            @click="toJoinEntry"
            style="width:380px;margin-top: 10px;"
            size="medium"
          >立即登录</el-button>
        </template>
        <template #subTitle>请登录后完成入驻申请，开启更多功能！</template>
      </el-result>
    </template>
  </div>
</template>

<script>
import { computed, defineComponent, onMounted, reactive, ref, toRaw } from "vue"
import { Steps, Step } from "ant-design-vue";

import { useUserRegisterMobilePhone } from "@/composables/userRegister";
import { useRoute, useRouter } from "vue-router";
import { utils } from "justom-share";
import request from "@/public/request";
import { ElMessage } from "element-plus";
import { useStore } from "vuex";
/**
 * 设置登录信息
 */
function registerStartRef(step, tokenRef) {

  function submitSuccess(token) {
    tokenRef.value = token;
    step.value += 1;
  }

  const registerRef = useUserRegisterMobilePhone(submitSuccess);





  return reactive(registerRef)
}


/**
 * 设置基本信息
 */
function registerFinishRef(step, tokenRef, realName) {
  const route = useRoute();
  const store = useStore();
  const data = ref({
    RealName: "",
    Email: "",
    Password: "",
    RepeatPassword: ""
  });
  const rules = reactive({
    RealName: [
      { required: true, message: '请输入您的真实姓名', trigger: 'blur' },
    ],
    Email: [
      { type: "email", message: '请输入正确的邮箱', trigger: 'blur' },
    ],
    Password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, message: '密码最少需要6位', trigger: 'blur' },
    ],
    RepeatPassword: [
      {
        validator: (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请再次输入密码'));
          } else if (value !== data.value.Password) {
            callback(new Error('两次输入密码不一致!'));
          } else {
            callback();
          }
        }, trigger: 'blur'
      }
    ],
  });

  const formRef = ref(null);


  /**
   * 提交
   */
  async function submit() {
    await formRef.value.validate();

    let res = await request.post("/api/AuthUser/FinishRegister", {
      ...toRaw(data.value),
      RegisterToken: tokenRef.value,
      BindToken:route.query.BindToken
    })
    if (!res.Status) {
      ElMessage.error(res.Message);
      return;
    }
    realName.value = data.value.RealName;


    // 读取用户信息
    await store.dispatch("auth/setTokenAndGetInfo",res.Data).catch(()=>{
      
    })

    ElMessage.success("注册成功");
    step.value += 1



  }




  return reactive({
    formRef,
    rules,
    data,
    submit
  })
}



export default defineComponent({
  components: {
    ASteps: Steps,
    AStep: Step
  },
  setup() {

    const router = useRouter();
    const route = useRoute();

    // 步骤
    const step = ref(0);
    // 手机号token
    const token = ref("");
  
    const realName = ref("");

    // 设置登录信息
    const registerStart = registerStartRef(step, token);
    // 设置基础信息
    const registerFinish = registerFinishRef(step, token, realName);

    // 成功界面 标题
    const successTitle = computed(() => {
      return `HI,${realName.value} 您的个人账户已经注册成功！`
    });


    function toJoinEntry() {
      router.push({
        path: "/join/entry",
        query: {
          showLogin: true
        }
      });
    }
    function toLogin() {
      router.push({
        path: "/",
        query: {
          showLogin: true
        }
      });
    }


    onMounted(() => {

      if (route.query.token) {

        token.value = decodeURI(route.query.token)
        step.value = 1;
      }


    });


    return {
      step,
      token,
      registerStart,
      registerFinish,
      toJoinEntry,
      toLogin,
      successTitle
    }
  }
})
</script>

<style lang='scss' scoped>
.container {
  background-color: #fff;
  padding: 40px 196px 64px 196px;
  border: 1px solid #e4e7ed;
  border-radius: 2px;
}
.content {
  margin: 0 auto;
  width: 380px;
  margin-top: 39px;
}
.field-title {
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #303133;
  margin-bottom: 20px;
}
.field {
  display: flex;
  justify-content: space-between;
}
.main-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
}
.toRegister {
  display: flex;
  justify-content: space-between;
  margin: 0 auto;
  line-height: 40px;
}

.submit-finish-btn {
  height: 40px;
  width: 100%;
  font-size: 16px;
}
.register-success {
  padding-top: 0px;
  padding-bottom: 0px;
  :deep(.el-result__subtitle) {
    color: #909399;
    font-size: 14px;
    margin-top: 16px;
  }
}
</style>